package com.wash.shoes.controller.pc;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import com.wash.shoes.entity.FactoryshelvesPo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.wash.shoes.entity.BrandPo;
import com.wash.shoes.mapper.ShopMapper;
import com.wash.shoes.request.BrandQueryRequest;
import com.wash.shoes.service.BrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import java.util.List;
import java.util.Objects;

@Api(tags = {"PC-店铺的品牌"})
@RestController
@RequestMapping("/pc/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    @ApiOperation("获取列表")
    @PostMapping("/list")
    public R<Page<BrandPo>> pageList(@RequestBody @Valid BrandQueryRequest bannerPo) {
        Page<BrandPo> page = brandService.page(bannerPo.toPage(),
                new LambdaQueryWrapper<BrandPo>()
                        .like(StringUtils.isNotEmpty(bannerPo.getKeywords()), BrandPo::getBrandName, bannerPo.getKeywords())
                        .or().like(StringUtils.isNotEmpty(bannerPo.getKeywords()), BrandPo::getAddress, bannerPo.getKeywords())
                        .eq(BrandPo::getStatus, 1).eq(StringUtils.isNotBlank(bannerPo.getPlatformType()), BrandPo::getPlatformType, bannerPo.getPlatformType())
        );

        return R.ok(page);
    }

    @ApiOperation("修改新增,有id为修改没有为新增")
    @PostMapping("/saveUpdate")
    public R<String> add(@RequestBody BrandPo bannerPo) {
        if (ObjectUtil.isEmpty(bannerPo.getId())) {
            bannerPo.setStatus(1);
            brandService.save(bannerPo);
        } else {
            brandService.updateById(bannerPo);
        }
        return R.ok();
    }

    @ApiOperation("逻辑删除修改 status为0")
    @GetMapping("/deleteByStatus")
    public R<BrandPo> deleteByStatus(@Valid @NotNull(message = "id不能为空") String id) {
        BrandPo bannerPo = brandService.getById(id);
        bannerPo.setStatus(0);
        brandService.updateById(bannerPo);
        return R.ok(bannerPo);
    }

    @ApiOperation("获取详情")
    @GetMapping("/getDetailById")
    public R<BrandPo> getDetailById(String theId) {
        BrandPo bannerPo = brandService.getById(theId);
        return R.ok(bannerPo);
    }

    @Autowired
    private ShopMapper shopMapper;

    @GetMapping("/getUserBrandList")
    @ApiOperation("根据用户id获取绑定的品牌")
    public R getUserBrandList() {
        // 用户账号绑定的类型
        Long userId = SecurityUtils.getUserId();
        List<String> brandIdList = shopMapper.getBrandIdByUserId(userId);
        boolean admin = SecurityUtils.isAdmin(userId);
        List<Map> result = Lists.newArrayList();
        if (admin) {
            List<BrandPo> brandPoList = brandService.list(new LambdaQueryWrapper<BrandPo>().eq(BrandPo::getStatus, 1));
            result = brandPoList.stream().map(brandPo -> {
                Map<String, Object> map = Maps.newHashMap();
                map.put("brandId", brandPo.getId());
                map.put("brandName", brandPo.getBrandName());
                return map;
            }).collect(Collectors.toList());
        } else if (CollectionUtil.isNotEmpty(brandIdList)) {
            List<BrandPo> brandPoList = brandService.list(new LambdaQueryWrapper<BrandPo>().eq(BrandPo::getStatus, 1).in(BrandPo::getId, brandIdList));
            result = brandPoList.stream().map(brandPo -> {
                Map<String, Object> map = Maps.newHashMap();
                map.put("brandId", brandPo.getId());
                map.put("brandName", brandPo.getBrandName());
                return map;
            }).collect(Collectors.toList());
        }
        return R.ok(result);
    }

    @ApiOperation("根据渠道获取品牌")
    @GetMapping("/getBrandByChannel")
    public R<List<BrandPo>> getBrandByChannel(String channel) {
        List<BrandPo> list = brandService.list(new LambdaQueryWrapper<BrandPo>()
                .eq(StringUtils.isNotEmpty(channel), BrandPo::getPlatformType, channel));
        return R.ok(list);
    }
}
